'use strict';

import IApp from './IApp';
import * as PIXI from 'pixi.js';
import { TweenMax } from 'gsap';

class _00Test implements IApp {
  private element: HTMLCanvasElement = null;

  private app: PIXI.Application = null;

  public create() {
    this.destory();
    const can = document.createElement('canvas');
    this.element = can;
    window.onresize = (e) => {
      this.onresize();
    };
    return can;
  }

  public start() {
    this.app = new PIXI.Application({
      view: this.element,
      resizeTo: this.element,
      antialias: true,
      transparent: false,
      resolution: 1,
      backgroundColor: 0x142220,
    });

    const loader = new PIXI.Loader();

    loader.add('images/dog.png').load((loader, res) => {
      console.log('loaded image.');
      const sp = new PIXI.Sprite(res['images/dog.png'].texture);
      this.app.stage.addChild(sp);

      const animate = TweenMax.to(sp.position, 2, { x: 500 });
      animate.play();
    });
  }

  public onresize(): any | void {
    if (this.app) {
      this.app.resize();
    }
  }

  public destory() {
    if (this.app) {
      this.app.destroy(true, {
        children: true,
        texture: true,
        baseTexture: true,
      });
      this.app = null;
    }
    if (this.element !== null) {
      const content = document.getElementById('content');
      if (content && content.children[0] == this.element) {
        content.removeChild(this.element);
      }
      this.element = null;
    }
  }
}

export default new _00Test();
